#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
except:
    pass
import jarryLibs.mayakit.MayaUtils as mu
reload(mu)
import elsLibs.AssetInfoFromFile as elsaif
reload(elsaif)
import jarryLibs.mayakit.JFileReference as jfr
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckRefAssetLastest(CheckBase):
    def __init__(self):
        super(CheckRefAssetLastest, self).__init__()
        pass

    def get_asset_reference_list(self):
        asset_refs = []
        ref_list = jfr.listReferences()
        for ref in ref_list:
            if 'Z:/ELS_Project/Assets/Character' in ref.path or \
               'Z:/ELS_Project/Assets/Prop' in ref.path or \
               'Z:/ELS_Project/Assets/Environment' in ref.path:
                if 'forani' in ref.path or 'forani' in ref.path or 'rig' in ref.path:
                    if ref.isLoaded():
                        asset_refs.append(ref)

        return asset_refs

    def check(self):
        result_log = 'Warning:'
        ref_list = self.get_asset_reference_list()
        error = 0
        for ref_node in ref_list:
            ref_path = ref_node.path
            asset = elsaif.AssetInfoFromFile(ref_path)
            lastest_file = asset.get_latest_version_file
            if ref_path.replace('\\', '/') != lastest_file.replace('\\', '/'):
                error += 1
                result_log += u'<br>查询到 %s_%s_%s 不是最新资产;' % (asset.get_shortType, asset.get_name, asset.get_step)
        if error == 0:
            result_log = u'Succeed:没有问题'

        return result_log

    def fix(self):
        result_log = 'Succeed:'
        error = 0
        import jarryLibs.mayakit.JFileReference as jrf

        import elsLibs.Asset as asst
        reference_list = self.get_asset_reference_list()
        reference_list = [ref for ref in reference_list if 'rig' in ref.path]
        for ref_node in reference_list:
            ref_path = ref_node.path
            # print ref_path
            # _asset = asst.Asset(ref_path)
            # _step = _asset.step
            # print 'publish_latest_version_file', _step.publish_latest_version_file

            asset = elsaif.AssetInfoFromFile(ref_path)
            lastest_file = asset.get_latest_version_file
            if ref_path.replace('\\', '/') != lastest_file.replace('\\', '/'):

                try:
                    ref_node.replaceWith(lastest_file)
                    result_log += u'<br>%s_%s_%s:该资产已经刷到最新;' % (asset.get_shortType, asset.get_name, asset.get_step)
                    log.info(result_log)
                except:
                    error += 1
                    result_log += u'<br>%s_%s_%s:该资产已经刷失败;' % (asset.get_shortType, asset.get_name, asset.get_step)
                    result_log = result_log.replace('Succeed:', 'Error:')
                    log.error(result_log)
        if error == 0:
            result_log = u'Succeed:没有问题'
            log.info(result_log)
            # log.error("Error: testError")
            # return 'Error: test errorr'
        return result_log
